package com.csaba.swing.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.csaba.connector.model.Customer;
import com.csaba.connector.model.Session;
import com.csaba.swing.MainWindow;
import com.csaba.swing.action.ActionFactory;
import com.csaba.swing.action.LogoutAction;
import com.csaba.swing.gui.util.DynamicColumnLayout;

@SuppressWarnings( "serial" )
public final class LogoutDialog extends JDialog
{
	//private static Logger logger = Logger.getLogger(LogoutDialog.class.getPackage().getName());

	private ButtonGroup optionGroup;
	private JComboBox sessionCombo;

	public LogoutDialog( Frame frame )
	{
		super(frame);

		setModal(true);
		setTitle(MainWindow.getString("LogoutDialog.title")); //$NON-NLS-1$
		setSize(450, 120);
		setResizable(false);
		setLocationRelativeTo(frame);

		//create input fields
		JPanel inputs = new JPanel(new DynamicColumnLayout(new int[] { DynamicColumnLayout.LEFT,
				DynamicColumnLayout.LEFT }));

		inputs.setBorder(BorderFactory.createEmptyBorder());

		sessionCombo = new JComboBox();
		List<Session> sessionList = GUIContext.getInstance().getSessions();
		for ( Session session : sessionList )
		{
			sessionCombo.addItem(new SessionItem(session));
		}

		JRadioButton allSessions = new JRadioButton(new AbstractAction(MainWindow.getString("LogoutDialog.radio.allSession")) //$NON-NLS-1$
		{
			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				sessionCombo.setEnabled(false);
			}

		});
		allSessions.setActionCommand("all-sessions"); //$NON-NLS-1$
		JRadioButton oneSession = new JRadioButton(new AbstractAction(MainWindow.getString("LogoutDialog.radio.oneSession")) //$NON-NLS-1$
		{
			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				sessionCombo.setEnabled(true);
			}

		});
		oneSession.setActionCommand("one-session"); //$NON-NLS-1$
		oneSession.setSelected(true);

		inputs.add(oneSession);
		inputs.add(sessionCombo);
		inputs.add(allSessions);
		inputs.add(new JLabel("")); //$NON-NLS-1$

		optionGroup = new ButtonGroup();
		optionGroup.add(allSessions);
		optionGroup.add(oneSession);

		//create buttons
		JPanel buttons = new JPanel(new FlowLayout());
		JButton logout = new JButton(new AbstractAction(MainWindow.getString("LogoutDialog.button.logout")) //$NON-NLS-1$
		{
			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				LogoutAction action = (LogoutAction) ActionFactory.getAction(LogoutAction.class);
				if ( optionGroup.getSelection().getActionCommand().equals("all-sessions") ) //$NON-NLS-1$
				{
					action.doLogout(null);
				}
				else
				{
					Session session = ( (SessionItem) sessionCombo.getSelectedItem() ).getSession();
					action.doLogout(session);
				}
				dispose();
			}
		});

		buttons.add(logout);

		JButton cancel = new JButton(new AbstractAction(MainWindow.getString("LogoutDialog.button.cancel")) //$NON-NLS-1$
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				dispose();
			}

		});
		buttons.add(cancel);

		//create content
		JPanel content = (JPanel) getContentPane();
		content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		content.setLayout(new BorderLayout());
		content.add(inputs, BorderLayout.CENTER);
		content.add(buttons, BorderLayout.SOUTH);

		doLayout();
	}

	class SessionItem
	{
		Session session;

		SessionItem( Session session )
		{
			this.session = session;
		}

		@Override
		public String toString()
		{
			Customer customer = session.getCustomer();
			if ( customer != null )
				return customer.getName() + " - " + session.getBank().getName(); //$NON-NLS-1$

			return MainWindow.getString("LogoutDialog.notLoggedIn") + session.getBank().getName(); //$NON-NLS-1$
		}

		public Session getSession()
		{
			return session;
		}

	}

}
